/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.geoviewer.jxmap.layerlist;

import java.beans.IntrospectionException;
import java.util.List;
import nl.cloudfarming.client.geoviewer.Layer;
import nl.cloudfarming.client.geoviewer.MapController;
import org.openide.nodes.ChildFactory;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;

/**
 * Fasctory for the creation of layer nodes in the layerlist
 *
 * @author Timon Veenstra
 */
public class LayerListChildFactory extends ChildFactory<Layer> {

    private final MapController mapController;

    public LayerListChildFactory(MapController mapController) {
        this.mapController = mapController;
    }

    public void refresh() {
        refresh(true);
    }

    @Override
    protected boolean createKeys(List<Layer> toPopulate) {
        toPopulate.addAll(this.mapController.getLayers());
        return true;
    }

    @Override
    protected Node createNodeForKey(Layer key) {
        try {
            //
            // create a beannode and override the getActions to return
            // the layers actions
            //
            return new LayerListLayerNode(key, mapController);

        } catch (IntrospectionException ex) {
            Exceptions.printStackTrace(ex);
            return null;
        }
    }

    @Override
    protected Node[] createNodesForKey(Layer key) {
        return new Node[]{createNodeForKey(key)};
    }
}
